package com.sunfeng.demo15;

import java.nio.ByteBuffer;

/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/11/6 18:00
 */
public class Test05 {
    public static void main(String[] args) {

        byte [] byteArrayIn1={1,2,3,4,5,6,7,8};
        byte [] byteArrayIn2={55,66,77,88};
        // 将数组放到缓冲区前八个的位置
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        byteBuffer.put(byteArrayIn1);
        //执行put()方法后位置发生变化，
        byteBuffer.position(2);
        //将数组55 66 77 88 中的 66 77 88 放入缓冲区的第三位

        byteBuffer.put(byteArrayIn2,1,3);
        System.out.println("=========");
        byte[] array = byteBuffer.array();
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
        System.out.println("==============");
        byteBuffer.position(1);
        byte [] byteArrayOut= new byte[byteBuffer.capacity()];

       byteBuffer.get(byteArrayOut, 3, 4);

        System.out.println("B======");
        for (int i = 0; i < byteArrayOut.length; i++) {
            System.out.print(byteArrayOut[i] + " ");
        }
    }
}
